package toy.keli.edic.data.config

import androidx.compose.runtime.Composable
import toy.keli.edic.data.UserStatus
import toy.keli.edic.ui.home.viewMode
import toy.keli.edic.ui.listenbook.ListenBookData
import toy.keli.edic.ui.main.config
import toy.keli.edic.ui.navigation.NavScreen
import toy.keli.edic.view.MPopMenu

class PageConf {
    // 定义接口，声明所有枚举类需要共享的属性和方法
    interface CommonEnum {
        var options: Any?    //选项
        var default: Any?    //默认值
        var myValue: Any?    //当前值缓存
        fun getEnumName(): String = (this as Enum<*>).name

        fun <T> getValue():T {
            if (myValue != null) return myValue as T
            val name = getEnumName();
            myValue = UserStatus.get {
                when (default) {
                    is Int -> it.getInt(name, default as Int)
                    is String -> it.getString(name, default as String)
                    is Boolean -> it.getBoolean(name, default as Boolean)
                    is Long -> it.getLong(name, default as Long)
                    is Float -> it.getFloat(name, default as Float)
                    else -> if (options == null) false else 0
                }
            }
            if (options is MPopMenu) {
                myValue = (options as? MPopMenu)?.items?.get(myValue as Int)?.value
            }
            return myValue!! as T
        }
        @Composable
        fun view(){
            val name = getEnumName()
            if(options==null){
                options = PageConf.options.get(name) ?: false
            }
            if(default==null){
                default = PageConf.default.get(name) ?: if(options == false) false else 0
            }

            if(options!=false){
                config.switchButton(name,options!!,default){
                    myValue = it.value
                    if(name== homePage.viewMode.name){
                        viewMode = it.value as MenuConf.mode;
                        NavScreen.openHome(0);
                    }
                }
            }else{
                config.booleanButton(name,default){
                    myValue = it
                }
            }
        }
    }

    enum class homePage(override var options:Any?=null, override var default:Any?=null, override var myValue:Any?=null) :CommonEnum{
        viewMode(options= MPopMenu(listOf(
            MPopMenu.dataClass("词典",value= MenuConf.mode.wordStudy),
            MPopMenu.dataClass("书本",value= MenuConf.mode.showArticle)
        )), 0),               //首页默认显示方式
        viewBookMode,           //文章页默认展示方式
        TitleClickEdit,         //标题长按可编辑
        SortWords,               //单词按照字母排序
        NextUnordered,          //单词排序乱序
        RemainViewMode,          //下一个单词保持当前的视图模式
        DefaultShowDetails,     //默认展示详情
        ShowMenuEvent(options= listOf("单击","双击","上拉"),0),          //显示菜单的事件
        DicType;                 //词典类型
    }


    enum class sGamePage (override var options:Any?=null, override var default:Any?=null, override var myValue:Any?=null) :CommonEnum{
        AutoNext,           //自动下一个
        LetterLen,          //单词长度过滤
        InLetter,           //是否包括包含词
    }

    enum class HistoryPage (override var options:Any?=null, override var default:Any?=null, override var myValue:Any?=null) :CommonEnum{
        defaultHistoryView,           //自动下一个
    }

    enum class DicType {
        en_en,     //英英
        en_cn,     //英汉
        encn       //英汉双语
    }


    companion object {
        val options = linkedMapOf(
            "LetterLen" to listOf(0,2,3,4,5,6,7,8,9,10,11).map { it.toString() },
            "DicType" to DicType.values().map { it.name },
            "viewMode" to MPopMenu.ViewModeMenu(),
            "viewBookMode" to listOf("read","listen"),
            "defaultHistoryView" to MPopMenu.HistoryTypes(),
        )
        val default = linkedMapOf(
            "InLetter" to true,
            "TitleClickEdit" to true,
            "DefaultShowDetails" to true,
            "DicType" to 2,
            "SortWords" to true,
            "RemainViewMode" to true
        )

//        public var ViewMode
//            get() = MenuConf.modeNow().name
//            set(v)  {
//                val mMenu = SettingConf.options["viewMode"]
//                mMenu?.show { k, v ->
//                    val vv = (v.value as MenuConf.mode).ordinal
//                    viewMode = MenuConf.modeNow(vv)
//                    NavScreen.openHome(0)
//                }
//            }

        fun getBoolean(t:homePage,defV:Boolean=false): Boolean {
            return UserStatus.get{
                return@get it.getBoolean(t.name,defV)
            }
        }
        fun getBoolean(t:sGamePage,defV:Boolean=false): Boolean {
            return UserStatus.get{
                return@get it.getBoolean(t.name,defV)
            }
        }
        fun getInt(t:HistoryPage): Int {
            return UserStatus.get{
                return@get it.getInt(t.name,0)
            }
        }
        fun getInt(t:sGamePage): Int {
            return UserStatus.get{
                return@get it.getInt(t.name,0)
            }
        }
        fun getInt(t:homePage,defv:Int=0): Int {
            return UserStatus.get{
                return@get it.getInt(t.name,defv)
            }
        }

    }
}
